<?php
namespace app\Capital\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

class Platformcapital extends Admin
{
    public function index()
    {
        $startDate=input('date1');
        $endDate=input('date2');
        $map=[];
        if($startDate && $endDate){
            $this->assign('startDate',$startDate);
            $this->assign('endDate',$endDate);
            $startDate = strtotime($startDate . ' 0:00:00');
            $endDate = strtotime($endDate . " 23:59:59");
            $map['createtime'] = ['between', [$startDate, $endDate]];
        }
        $order=Db::name("order")->select();
        $commision_money=array_sum(array_column($order,"commision_money"));
        $shipping_price=array_sum(array_column($order,"shipping_price"));
        $withdrawals=Db::name("withdrawals")->where('user_type',3)->select();
        $platform_withdrawals=array_sum(array_column($withdrawals,"num"));
        $platform_closing_balance=$commision_money+$shipping_price-$platform_withdrawals;
        $data['commision_money']=getPrice($commision_money);
        $data['platform_withdrawals']=getPrice($platform_withdrawals);
        $data['shipping_price']=getPrice($shipping_price);
        $data['platform_closing_balance']=getPrice($platform_closing_balance);
       // dump($data);exit();
        $this->assign('data',$data);
        return $this->fetch('index');
    }

    public function settlement()
    {
        $startDate=input('date1');
        $endDate=input('date2');
        $search=input('search');
        $map=[];
        if($startDate && $endDate){
            $startDate = strtotime($startDate . ' 0:00:00');
            $endDate = strtotime($endDate . " 23:59:59");
            $map['createtime'] = ['between', [$startDate, $endDate]];
        }
        if($search){
            $map["settlement|memberid|member_name|no"]=['like',"%$search%"];
        }
        $status=input('status');
        if(99==$status){
            $map['status']=0;
        }
        if($map){
            $withdrawals=Db::name('platform_settlement')->where($map)->order('settlementid desc')->paginate(5);
        }else{
            $withdrawals=Db::name('platform_settlement')->order('settlementid desc')->paginate(5);
        }
        $page=$withdrawals->render();
        return ZBuilder::make('mix')
            ->setPageTitle('检索')
            ->addText('search', '条件')
            ->addSelect('status','结算状态','',[0 => '待结算', 1 => '已结算', 2 => '已作废'])
            ->addDaterange('date1,date2', '日期范围')
            ->addBtn('<button type="submit" class="btn  btn-primary btn-rounded">确定搜索</button>')
            ->hideBtn('submit,back')
            ->layout(['search' => 2,'status'=>2, 'date1' => 4, 'date2' => 4, 'form_group_btnGroup' => 2])
            ->setPageTitle('结算记录')// 设置页面标题
            ->setPrimaryKey('settlementid')
            ->addColumns([ // 批量添加列
                ['settlementid', 'ID'],
                ['no', '单据号'],
                ['member_name', '客户名称'],
                ['order_amount', '订单金额'],
                ['taxation','税费'],
                ['commission', '平台佣金'],
                ['settlement_money', '店铺结算金额'],
                ['createtime', '创建时间', 'datetime']
            ])
            ->addColumn('status', '状态', 'status', '', [0 => '待结算', 1 => '已结算', 2 => '已作废'])
            ->addColumn('right_button', '操作', 'btn')
            ->addTopButton('enable', ['title' => '批量审核', 'table' => 'platform_settlement'])
            ->addTopButton('custom', ['title' => '待审核', 'icon' => 'fa fa-fw fa-cogs', 'href' => './settlement.html?status=99'])
            ->addRightButton('edit', ['title' => '作废', 'icon' => 'fa fa-wrench'])// 批量添加右侧按钮
            ->addRightButton('custom', ['title' => '查看', 'icon' => 'fa fa-eye'])// 批量添加右侧按钮
            ->setRowList($withdrawals)
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }
    //作废
    public function edit($record = []){
        dump(input());
    }
}